﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Windows.Forms;
using Assist;
using Autodesk.Revit.DB;
using YArchitech.Controls;
using YArchitech.DataModel;
using YArchitech.LIB;
using YArchitech.LIB.YJKControls;
using YArchitech.MEP;
using YArchitech.Revit;

namespace YArchitech.Plumbing
{
	public partial class FormReplaceSprinkler : System.Windows.Forms.Form
	{
		public int CurrentBtn
		{
			get
			{
				return this.currentBtn;
			}
		}

		public FamilySymbol Fsymbol { get; set; }

		public FormReplaceSprinkler(Autodesk.Revit.DB.Document doc)
		{
			this.InitializeComponent();
			new YJKProcessTools().ShowInTaskbarChange(this);
			this.doc = doc;
			base.Icon = Resource.YArchitechIcon;
			this.InitCurrentProject();
			this.btnHy.BackColor = System.Drawing.Color.White;
			this.currentBtn = 0;
			this.btnproj.BackColor = System.Drawing.Color.Transparent;
			this.panelHy.Visible = true;
			this.panelProj.Visible = false;
		}

		private void InitCurrentProject()
		{
			this.tvwProject.Nodes.Add("喷头");
			HyProjectFamily.BingTvwFamilysTwoLevel(HyProjectFamily.GetFamilys(BuiltInCategory.OST_Sprinklers, this.doc), this.tvwProject);
		}

		protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
		{
			int num = 256;
			int num2 = 260;
			if ((msg.Msg == num | msg.Msg == num2) && keyData == Keys.Escape)
			{
				base.DialogResult = DialogResult.Cancel;
			}
			return false;
		}

		private void picbox_Click(object sender, EventArgs e)
		{
			this.name = this.lbfname.Text;
			FormChooseSprinkler formChooseSprinkler = new FormChooseSprinkler(this.name, this.listInfos);
			formChooseSprinkler.Text = "选择喷头";
			formChooseSprinkler.Owner = this;
			if (formChooseSprinkler.ShowDialog() == DialogResult.OK && this.name != formChooseSprinkler.ImageName && formChooseSprinkler.ImageName != "")
			{
				this.name = formChooseSprinkler.ImageName;
				this.lbfname.Text = this.name;
				foreach (ListItem listItem in this.listInfos)
				{
					if (listItem.Name == this.name)
					{
						this.id = int.Parse(listItem.Tag.ToString());
						break;
					}
				}
			}
		}

		private void FormLaySprinklerHead_Load(object sender, EventArgs e)
		{
			this.InitParameterSetting();
			switch (this.familyCategory)
			{
			case 1:
				this.rbtUp.Checked = true;
				break;
			case 2:
				this.rbtDown.Checked = true;
				break;
			case 3:
				this.rbtCe.Checked = true;
				break;
			case 4:
				this.rbtShuimu.Checked = true;
				break;
			}
			this.GetfamilyNameFromData();
			this.lbfname.Text = this.familyName;
			if (this.listname.Count > 0)
			{
				foreach (ListItem listItem in this.listname)
				{
					if (listItem.Name == this.name)
					{
						this.id = int.Parse(listItem.Tag.ToString());
						break;
					}
				}
			}
			if (this.lbSymbol.Items.Count > 0)
			{
				this.lbSymbol.SelectedItem = this.familySymbol;
			}
		}

		private void GetSprinklerPara()
		{
			if (this.lbSymbol.SelectedItem.ToString() != "" && this.id > 0)
			{
				this.sprinklersPara = new SprinklersParam();
				this.sprinklersPara.SymbolName = this.lbSymbol.SelectedItem.ToString();
				this.sprinklersPara.Id = this.id;
				return;
			}
			YJKMessageBox.Show("参数都不能为空");
		}

		private void InitParameterSetting()
		{
			UserSetting userSetting = new UserSetting();
			if (userSetting.GetValue("sprinklersRetangle", "familyCategory") != "")
			{
				this.familyCategory = int.Parse(userSetting.GetValue("sprinklersRetangle", "familyCategory"));
			}
			else
			{
				this.familyCategory = 1;
			}
			this.familyName = userSetting.GetValue("sprinklersRetangle", "familyName");
			this.familySymbol = userSetting.GetValue("sprinklersRetangle", "familySymbol");
		}

		private void UserSettingWrite()
		{
			try
			{
				UserSetting userSetting = new UserSetting();
				userSetting.WriteValue("sprinklersRetangle", "familyCategory", this.familyCategory.ToString());
				userSetting.WriteValue("sprinklersRetangle", "familyName", this.lbfname.Text.Trim());
				userSetting.WriteValue("sprinklersRetangle", "familySymbol", this.lbSymbol.SelectedItem.ToString());
			}
			catch (Exception)
			{
			}
		}

		private void rbtUp_CheckedChanged(object sender, EventArgs e)
		{
			if (this.rbtUp.Checked)
			{
				this.familyCategory = 1;
				this.GetfamilyNameFromData();
				this.lbfname.Text = this.listname[0].Name;
			}
		}

		private void rbtDown_CheckedChanged(object sender, EventArgs e)
		{
			if (this.rbtDown.Checked)
			{
				this.familyCategory = 2;
				this.GetfamilyNameFromData();
				this.lbfname.Text = this.listname[0].Name;
			}
		}

		private void rbtCe_CheckedChanged(object sender, EventArgs e)
		{
			if (this.rbtCe.Checked)
			{
				this.familyCategory = 3;
				this.GetfamilyNameFromData();
				this.lbfname.Text = this.listname[0].Name;
			}
		}

		private void rbtShuimu_CheckedChanged(object sender, EventArgs e)
		{
			if (this.rbtShuimu.Checked)
			{
				this.familyCategory = 4;
				this.GetfamilyNameFromData();
				this.lbfname.Text = this.listname[0].Name;
			}
		}

		private void GetfamilyNameFromData()
		{
			this.listname = this.sfdr.GetAllName("Sprinkler", this.familyCategory);
			this.listInfos = this.sfdr.GetAllNameAndImage("Sprinkler", this.familyCategory);
		}

		private void UptadaFamilySybol(string szData)
		{
			this.lbSymbol.Items.Clear();
			foreach (string text in szData.Split(new char[]
			{
				';',
				',',
				'，'
			}))
			{
				if (text.Trim() != "")
				{
					this.lbSymbol.Items.Add(text);
				}
			}
			if (this.lbSymbol.Items != null && this.lbSymbol.Items.Count > 0)
			{
				this.lbSymbol.SelectedIndex = 0;
			}
		}

		private void lbfname_TextChanged(object sender, EventArgs e)
		{
			this.LbfnameTextChanged();
		}

		private void LbfnameTextChanged()
		{
			SpaceFamilyDataReader spaceFamilyDataReader = new SpaceFamilyDataReader();
			this.id = 0;
			if (this.listname.Count > 0)
			{
				foreach (ListItem listItem in this.listname)
				{
					if (this.lbfname.Text.Trim() == listItem.Name)
					{
						this.id = int.Parse(listItem.Tag.ToString());
					}
				}
			}
			foreach (ListItem listItem2 in spaceFamilyDataReader.GetInfoById("Sprinkler", this.id))
			{
				if (listItem2.Name == "Image")
				{
					if (File.Exists(listItem2.Value))
					{
						this.picbox.Image = new Bitmap(listItem2.Value);
					}
					else
					{
						this.picbox.Image = null;
					}
				}
				else if (listItem2.Name == "FamilySymbol")
				{
					this.UptadaFamilySybol(listItem2.Value);
				}
			}
		}

		private void btnReplace_Click(object sender, EventArgs e)
		{
			this.GetSprinklerPara();
			if (this.sprinklersPara != null)
			{
				this.UserSettingWrite();
				base.DialogResult = DialogResult.OK;
			}
		}

		private void btnCancle_Click_1(object sender, EventArgs e)
		{
			base.DialogResult = DialogResult.Cancel;
		}

		private void tvwProject_AfterSelect(object sender, TreeViewEventArgs e)
		{
			if (this.tvwProject.SelectedNode == null)
			{
				return;
			}
			this.tvwProject.SelectedNode.BackColor = System.Drawing.Color.DodgerBlue;
			this.tvwProject.SelectedNode.ForeColor = System.Drawing.Color.White;
			if (this.tvwProject.SelectedNode.Level == 0)
			{
				this.lbSymbol.Items.Clear();
				return;
			}
			Family family = this.tvwProject.SelectedNode.Tag as Family;
			this.lbSymbol.Items.Clear();
			this.fsList.Clear();
			this.fsList = family.GetSymbols();
			if (this.fsList.Count < 1)
			{
				return;
			}
			foreach (FamilySymbol familySymbol in this.fsList)
			{
				this.lbSymbol.Items.Add(familySymbol.Name);
			}
			this.lbSymbol.SelectedIndex = 0;
		}

		private void tvwProject_BeforeSelect(object sender, TreeViewCancelEventArgs e)
		{
			if (this.tvwProject.SelectedNode != null)
			{
				this.tvwProject.SelectedNode.BackColor = System.Drawing.Color.White;
				this.tvwProject.SelectedNode.ForeColor = System.Drawing.Color.Black;
			}
		}

		private void btnHy_Click(object sender, EventArgs e)
		{
			this.LbfnameTextChanged();
			this.btnHy.BackColor = System.Drawing.Color.White;
			this.currentBtn = 0;
			this.btnproj.BackColor = System.Drawing.Color.Transparent;
			this.panelHy.Visible = true;
			this.panelProj.Visible = false;
		}

		private void btnproj_Click(object sender, EventArgs e)
		{
			this.btnHy.BackColor = System.Drawing.Color.Transparent;
			this.currentBtn = 1;
			this.btnproj.BackColor = System.Drawing.Color.White;
			this.panelHy.Visible = false;
			this.panelProj.Visible = true;
			if (this.tvwProject.SelectedNode != null)
			{
				if (this.tvwProject.SelectedNode == this.tvwProject.TopNode)
				{
					this.lbSymbol.Items.Clear();
					return;
				}
				TreeNode selectedNode = this.tvwProject.SelectedNode;
				this.tvwProject.SelectedNode = this.tvwProject.Nodes[0];
				if (this.tvwProject.Nodes[0].Nodes.Count < 1)
				{
					return;
				}
				this.tvwProject.SelectedNode = selectedNode;
				return;
			}
			else
			{
				this.tvwProject.SelectedNode = this.tvwProject.Nodes[0];
				if (this.tvwProject.Nodes[0].Nodes.Count < 1)
				{
					return;
				}
				this.tvwProject.SelectedNode = this.tvwProject.Nodes[0].Nodes[0];
				return;
			}
		}

		private void lbSymbol_SelectedIndexChanged(object sender, EventArgs e)
		{
			if (this.currentBtn == 1)
			{
				this.Fsymbol = this.fsList.FirstOrDefault((FamilySymbol q) => q.Name == this.lbSymbol.SelectedItem.ToString());
			}
		}

		public SprinklersParam sprinklersPara;

		public LayAreaParameter layAreaParameter = new LayAreaParameter();

		private HVACUserSetting userSettingS = new HVACUserSetting();

		private List<ListItem> listInfos = new List<ListItem>();

		private string name = "";

		public int familyCategory = 1;

		public int familyConnStyle = 1;

		private string familyName = "";

		private string familySymbol = "";

		private int id;

		private List<ListItem> listname = new List<ListItem>();

		private Document doc;

		private int currentBtn;

		private List<FamilySymbol> fsList = new List<FamilySymbol>();

		private SpaceFamilyDataReader sfdr = new SpaceFamilyDataReader();
	}
}
